# -*- coding: utf-8 -*-

from flask_restplus import fields, marshal

from application import dummy_ns
from . import namespaces as ns


class UserField(fields.Raw):
    def format(self, user):
        try:
            if user.user_type == 'admin':
                return marshal(user, admin)
            elif user.user_type == 'wechat':
                return marshal(user, wechat_user)
        except:
            return None


wechat_user = ns.auth.clone(
    'WechatUser',
    dummy_ns.base_wechat_user,
)

admin = ns.auth.model(
    'Admin',
    {
        'id': fields.Integer(attribute='id_'),
        'username': fields.String,
    }
)

login_resp = ns.auth.model(
    'LoginResp',
    {
        'token': fields.String,
        'userInfo': UserField(attribute='user_info'),
    }
)